package com.ruoyi.members.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.CommonConstants;
import com.ruoyi.common.constant.MembersConstant;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.MembersStatus;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.members.mapper.SysMembersMapper;
import com.ruoyi.members.domain.SysMembers;
import com.ruoyi.members.service.ISysMembersService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-16
 */
@Service
@Transactional
public class SysMembersServiceImpl extends ServiceImpl<SysMembersMapper,SysMembers> implements ISysMembersService
{
    @Autowired
    private SysMembersMapper sysMembersMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysRoleService sysRoleService;


    /**
     * 查询会员
     * 
     * @param membersId 会员主键
     * @return 会员
     */
    @Override
    public SysMembers selectSysMembersByMembersId(Long membersId)
    {
        return sysMembersMapper.selectSysMembersByMembersId(membersId);
    }

    /**
     * 查询会员列表
     * 
     * @param sysMembers 会员
     * @return 会员
     */
    @Override
    public List<SysMembers> selectSysMembersList(SysMembers sysMembers)
    {
        return sysMembersMapper.selectSysMembersList(sysMembers);
    }

    /**
     * 新增会员
     * 
     * @param sysMembers 会员
     * @return 结果
     */
    @Override
    public int insertSysMembers(SysMembers sysMembers)
    {
        sysMembers.setCreateTime(DateUtils.getNowDate());
        return sysMembersMapper.insertSysMembers(sysMembers);
    }

    /**
     * 修改会员
     * 
     * @param sysMembers 会员
     * @return 结果
     */
    @Override
    public int updateSysMembers(SysMembers sysMembers)
    {
        sysMembers.setUpdateTime(DateUtils.getNowDate());
        return sysMembersMapper.updateSysMembers(sysMembers);
    }

    /**
     * 批量删除会员
     * 
     * @param membersIds 需要删除的会员主键
     * @return 结果
     */
    @Override
    public int deleteSysMembersByMembersIds(Long[] membersIds)
    {
        return sysMembersMapper.deleteSysMembersByMembersIds(membersIds);
    }

    /**
     * 删除会员信息
     * 
     * @param membersId 会员主键
     * @return 结果
     */
    @Override
    public int deleteSysMembersByMembersId(Long membersId)
    {
        return sysMembersMapper.deleteSysMembersByMembersId(membersId);
    }

    /**
     * 新增教职工
     * @param sysMembers
     * @return
     */
    @Override
    public MembersStatus saveSysMembers(SysMembers sysMembers) {

        // 工号不能重复（会员表   工号字段）
        List<SysMembers> list = list(new LambdaQueryWrapper<>(SysMembers.class).eq(SysMembers::getMembersNum, sysMembers.getMembersNum()));
        if(list.size() > 0){
            return MembersStatus.MEMBERS_NUM_EXIST;
        }

        // 账号不能重复（用户表  账号字段）
        List<SysUser> list1 = sysUserService.list(new LambdaQueryWrapper<>(SysUser.class).eq(SysUser::getUserName, sysMembers.getMembersNum()));
        if(list1.size() > 0){
            return MembersStatus.ACCOUNT_EXIST;
        }

        // 教职工角色是否存在
        SysRole sysRole = new SysRole();
        sysRole.setRoleName(MembersConstant.TEACHER_ROLE_NAME);
        List<SysRole> sysRoles = sysRoleService.selectRoleList(sysRole);
        if(sysRoles.size() == 0){
            return MembersStatus.TEACHER_ROLE_NOT_EXIST;
        }

        // 新增用户  用户表
        SysUser sysUser = new SysUser();
        sysUser.setUserName(sysMembers.getMembersNum().toString());
        sysUser.setNickName(sysMembers.getMembersName());
        sysUser.setDeptId(MembersConstant.NO_DEPT);
        sysUser.setEmail(sysMembers.getMembersEmail());
        sysUser.setPhonenumber(sysMembers.getMembersTel());
        sysUser.setSex(sysMembers.getMembersSex());
        sysUser.setAvatar(sysMembers.getMembersImg());
        sysUser.setPassword(SecurityUtils.encryptPassword(CommonConstants.DEFAULT_PASSWORD));
        sysUser.setCreateBy(SecurityUtils.getUsername());
        boolean save = sysUserService.save(sysUser);
        if(!save){
            return MembersStatus.FAIL;
        }

        // 教职工
        sysMembers.setUserId(sysUser.getUserId());
        sysMembers.setCreateBy(SecurityUtils.getUsername());
        sysMembers.setDeptId(MembersConstant.NO_DEPT);
        sysMembers.setSocietyId(MembersConstant.NO_ASSOCIATION);
        sysMembers.setMembersIsStatus(MembersConstant.NOT_MEMBERS);
        /**
         * 工会、协会、是否是会员
         */
        boolean save1 = save(sysMembers);
        if(!save1){
            return MembersStatus.TEACHER_ADD_FAIL;
        }

        // 用户角色分配
        sysUserService.insertUserAuth(sysUser.getUserId(),new Long[]{sysRoles.get(0).getRoleId()});

        return MembersStatus.SUCCESS;
    }


    /**
     * 删除教职工
     * @param membersIds
     * @return
     */
    @Override
    public MembersStatus deleteSysTeaching(Long[] membersIds) {

        // 批量查询教职工信息
        LambdaQueryWrapper<SysMembers> sysMembersLambdaQueryWrapper = new LambdaQueryWrapper<>(SysMembers.class);
        sysMembersLambdaQueryWrapper.in(SysMembers::getMembersId,membersIds);
        List<SysMembers> list = list(sysMembersLambdaQueryWrapper);
        if(list.size() == 0){
            return MembersStatus.TEACHER_NOT_EXIST;
        }

        boolean b = removeByIds(Arrays.asList(membersIds));
        if(!b){
            return MembersStatus.DELETE_FAIL;
        }

        // 删除用户表
        List<Long> userIds = list.stream().map(SysMembers::getUserId).collect(Collectors.toList());
        boolean b1 = sysUserService.removeByIds(userIds);
        if(!b1){
            return MembersStatus.DELETE_FAIL;
        }

        SysRole sysRole = new SysRole();
        sysRole.setRoleName(MembersConstant.TEACHER_ROLE_NAME);
        List<SysRole> sysRoles = sysRoleService.selectRoleList(sysRole);
        if(sysRoles.size() == 0){
            return MembersStatus.TEACHER_ROLE_NOT_EXIST;
        }
        // 删除用户角色表
        sysRoleService.deleteAuthUsers(sysRoles.get(0).getRoleId(),userIds.toArray(new Long[]{}));


        return MembersStatus.SUCCESS;
    }
}
